import 'package:enjoy_plus/pages/home/index.dart';
import 'package:enjoy_plus/pages/mine/index.dart';
import 'package:enjoy_plus/utils/eventbus.dart';
import 'package:enjoy_plus/utils/token.dart';
import 'package:flutter/material.dart';

class TabBarPage extends StatefulWidget {
  const TabBarPage({super.key});

  @override
  State<TabBarPage> createState() => _TabBarPageState();
}

class _TabBarPageState extends State<TabBarPage> {
  // 高亮下标
  int currentIndex = 0;

  @override
  void initState() {
    super.initState();
    initToken();

    // 注册(监听)登出事件
    eventBus.on<LogoutEvent>().listen((event) {
      // 跳转登录页面
      Navigator.pushNamed(context, '/login');
    });
  }

  initToken() async {
    await TokenManager().init();
    print(TokenManager().getToken());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      // SafeArea 安全区
      body: Container(
        // IndexedStack 索引栈，用于切换页面
        child: IndexedStack(
          index: currentIndex,
          children: [
            const HomePage(),
            MinePage(currentIndex: currentIndex),
          ],
        ),
      ),
      // 底部导航栏
      bottomNavigationBar: BottomNavigationBar(
        onTap: (int index) {
          setState(() {
            currentIndex = index;
          });
        },
        currentIndex: currentIndex,
        type: BottomNavigationBarType.fixed,
        selectedItemColor: const Color.fromARGB(255, 99, 144, 172),
        unselectedItemColor: Colors.grey,
        items: [
          BottomNavigationBarItem(
            icon: Image.asset(
              'assets/tabs/home_default.png',
              width: 32,
              height: 32,
            ),
            activeIcon: Image.asset(
              'assets/tabs/home_active.png',
              width: 32,
              height: 32,
            ),
            label: "首页",
          ),
          BottomNavigationBarItem(
            icon: Image.asset(
              'assets/tabs/my_default.png',
              width: 32,
              height: 32,
            ),
            activeIcon: Image.asset(
              'assets/tabs/my_active.png',
              width: 32,
              height: 32,
            ),
            label: "我的",
          ),
        ],
      ),
    );
  }
}
